import Vue from 'vue'
import VueRouter from 'vue-router'
import * as util from '@/util'
import console from '@/router/consoleRouter'

let consoles = []
consoles = consoles.concat(console)

Vue.use(VueRouter)

const routes = [
  {
    name: 'login',
    path: '/login',
    component: () => import('@/views/common/Login.vue'),
    meta: {
      title: '用户登录'
    }
  },
  {
    name: 'welcome',
    path: '/welcome',
    component: () => import('@/views/common/Welcome.vue')
  },
  {
    // 后台管理
    name: 'console',
    path: '/console',
    component: () => import('@/views/Console.vue'),
    children: consoles
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.matched.length === 0) {
    if (util.getToken()) {
      next('/console')
    } else {
      next('/login')
    }
  } else {
    next()
  }
})

export default router
